﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using Common;

namespace TokenGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public ElectrosEntities Entity = new ElectrosEntities();

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            lblMsg.Text = txtToken.Text = "";

            if ((txtUsername.Text == String.Empty) || (txtPassword.Text == String.Empty) || (txtPIN.Text == String.Empty))
            {
                lblMsg.Visible = true;
                lblMsg.Text = "Please input a username, password and PIN!";
            }
            else
            {
                string username = txtUsername.Text.ToLower();
                string password = txtPassword.Text;
                int pin = Convert.ToInt32(txtPIN.Text);

                User user = Entity.Users.SingleOrDefault(u => u.Username.ToLower() == username && u.Password == password && u.PIN == pin);
                if (user != null)
                {
                    Encryption enc = new Encryption();
                    string token = enc.EncryptTripleDES(password, txtPIN.Text);
                    lblToken.Visible = txtToken.Visible = true;
                    txtToken.Text = token;

                    try
                    {
                        SendEmail(user.Email, user.Name, token);
                        MessageBox.Show("Your token has also been sent to you via e-mail", "Token", MessageBoxButtons.OK);
                    }
                    catch (SmtpException ex)
                    {
                        lblMsg.Text = ex.Message;
                    }
                }
                else
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "Invalid username, password or PIN!";
                }
            }
        }

        private void SendEmail(string email, string userName, string token)
        {
            MailMessage mm = new MailMessage();
            mm.To.Add(email);
            mm.From = new MailAddress("vanityclothing01@gmail.com", "Electros Ltd");
            mm.Bcc.Add("vanityclothing01@gmail.com");
            mm.Subject = "Your account details";
            mm.Body = "Dear " + userName + ", <br/><br/> Thank you for creating an account with our website.";
            mm.Body += "<br/> Your token is: " + token;
            mm.Body += "<br/><br/> Yours sincerely, <br/> <b>Electros Ltd</b>";
            mm.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential("vanityclothing01@gmail.com", "vanityadmin");
            client.Send(mm);
        }
    }
}
